package Test.clone;

import java.util.Arrays;
import java.util.Comparator;

public class CloneTest {
//    public static class Money implements Cloneable{
//        public double m = 9.9;
//        @Override
//        protected Object clone() throws CloneNotSupportedException {
//            return super.clone();
//        }
//    }
    public static class Person implements Cloneable{
        private String name;
        private int age;
        //public Money money = new Money();
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
//            Person p = (Person) super.clone();
//            p.money = (Money) this.money.clone();
//            return p;
        }
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person("zhangsan",10);
        Person p2 = (Person) p1.clone();
        System.out.println(p1);
        System.out.println(p2);
//        System.out.println(p1.money.m);
//        System.out.println(p2.money.m);
//        p2.money.m = 99.9;
//        System.out.println(p1.money.m);
//        System.out.println(p2.money.m);
    }
    package deap;

import java.util.Arrays;

    public class PriorityQueue {
        public int[] elem = new int[11];
        public int usedSize;

        public PriorityQueue() {

        }
        public void print(){
            for (int i = 0;i<usedSize;i++){
                System.out.print(elem[i]+" ");
            }
        }

        /**
         * 建堆的时间复杂度：
         *
         * @param array
         */
        public void createHeap(int[] array) {
            if (array.length > elem.length) {
                // 扩容逻辑
                elem = Arrays.copyOf(elem, array.length);
            }
            for (int i = 0; i < array.length; i++) {
                this.elem[i] = array[i];
                this.usedSize++;
            }
            for (int i = (usedSize-2)/2; i >=0; i--) {
                shiftDown(i,usedSize);
            }

        }

        /**
         *
         * @param root 是每棵子树的根节点的下标
         * @param len  是每棵子树调整结束的结束条件
         * 向下调整的时间复杂度：O(logn)
         */
        private void shiftDown(int root,int len) {
            int child = root*2+1;
            while (child<len){
                if(child+1<len && elem[child+1]>elem[child]){
                    child++;
                }
                if(elem[child]<=elem[root]){
                    break;
                }
                int tmp = elem[child];
                elem[child] = elem[root];
                elem[root] = tmp;
                root = child;
                child = root*2+1;
            }
        }


        /**
         * 入队：仍然要保持是大根堆
         * @param val
         */
        //在usedSize位置插入数据 再向上调整
        public void push(int val) {
            elem[usedSize++] =50;
            shiftUp(usedSize-1);

        }

        private void shiftUp(int child) {
            int parent = (child-1)/2;
            while (parent>=0){
                if(elem[parent]>=elem[child]){
                    break;
                }
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                child = parent;
                parent = (child-1)/2;
            }
        }

        public boolean isFull() {
            return usedSize==elem.length;
        }

        /**
         * 出队【删除】：每次删除的都是优先级高的元素
         * 仍然要保持是大根堆
         */

        //先把对顶元素和最后一个元素交换，use--，堆顶再向下调整一次
        public void pollHeap() {
            elem[0] = elem[--usedSize];
            shiftDown(0,usedSize);
        }

        public boolean isEmpty() {
            return usedSize==0;
        }

        /**
         * 获取堆顶元素
         * @return
         */
        public int peekHeap() {
            return elem[elem.length-1];
        }
    }


}

